//
//  NetworkingManager.swift
//  颖姐姐
//
//  Created by wu on 2017/6/22.
//  Copyright © 2017年 wu. All rights reserved.
//

import UIKit
import AFNetworking
import Alamofire


enum YHTTPMethod {
    case GET
    case POST
}

class NetworkingManager: AFHTTPSessionManager {
    
    static let shared = { () -> NetworkingManager in
        
        let  instance = NetworkingManager()
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return instance
        
    }()
    
    
    /// 网络请求方法，GET/POST
    ///
    /// - Parameters:
    ///   - method: GET / POST ， 此处给默认值GET，如果是GET请求可以不传参，类似C语言中的那种默认值
    ///   - URLString: 请求地址
    ///   - parameter: 请求参数
    ///   - finish: 回调
    func request(method:YHTTPMethod = .GET,URLString:String,parameter:[String:AnyObject]?,finish:@escaping (_ json:AnyObject?,_ isSuccess:Bool)->()) {
        
        let success  = {(task:URLSessionDataTask,json:Any?)->() in
            finish(json as AnyObject?,true)
        }
        let  failure = {(task:URLSessionDataTask?,error:Error)->() in
            print(error)
            finish(nil,false)
        }
        if method == .GET {
            get(URLString, parameters: parameter, progress: nil, success: success, failure: failure)
        }else{
            post(URLString, parameters: parameter, progress: nil, success: success, failure: failure)
        }
    }
}


class AlamofireManager{
    
    static let shared = AlamofireManager()
    
    
    /// <#Description#>
    ///
    /// - Parameters:
    ///   - url: <#url description#>
    ///   - method: <#method description#>
    ///   - parameters: <#parameters description#>
    ///   - finnish: <#finnish description#>
    func loadDic(url:String,method:YHTTPMethod,parameters: [String:AnyObject]?,finnish:@escaping (_ json:[String:AnyObject]?, _ isSuccess:Bool)->()) {
        
        let gp = (method == YHTTPMethod.GET) ? HTTPMethod.get : HTTPMethod.post
        
        Alamofire.request(url, method: gp, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            print(response.result.value ?? 1)
            
            guard let result = response.result.value as? [String:AnyObject] else{
                finnish(nil ,false)
                return
            }
            
            finnish(result,true)
        }
    }
    
    func loadArr(url:String,method:HTTPMethod,parameters: [String:AnyObject]?,finnish:@escaping (_ json:[AnyObject]?, _ isSuccess:Bool)->()) {
        
        
        Alamofire.request(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
//            print(response.result.value ?? 1)
            
            guard let result = response.result.value as? [AnyObject] else{
                finnish(nil ,false)
                
                return
            }
            
            finnish(result,true)
        }
    }
    
    fileprivate init(){}

}















